/*
 * Copyright (C) 2009 Leandro de Oliveira Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.japa.interceptor.autopersist;

import static com.google.code.japa.util.AnnotationUtils.*;

import java.io.*;
import java.lang.reflect.*;

import org.springframework.aop.support.*;

import com.google.code.japa.annotation.*;
import com.google.code.japa.util.*;

/**
 * Pointcut to intercept all methods annotated with {@link com.google.code.japa.annotation.AutomaticPersistence}
 * or methods of classes annotated with {@link com.google.code.japa.annotation.AutomaticPersistence}.
 * 
 * @author Leandro Aparecido
 * @since 1.0
 * @see com.google.code.japa.annotation.AutomaticPersistence
 * @see com.google.code.japa.interceptor.autopersist.AutomaticPersistenceAdvisor
 */
public class AutomaticPersistencePointcut extends StaticMethodMatcherPointcut implements Serializable {

	private static final long serialVersionUID = 4968763938605434857L;

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public boolean matches(Method method, Class targetClass) {
		boolean matches;

		if (JavaBeanUtils.isGetter(method) || JavaBeanUtils.isSetter(method)) {
			matches = false;
		} else {
			matches = getAnnotation(targetClass, AutomaticPersistence.class) != null ||
					  getAnnotation(method, AutomaticPersistence.class) != null;
		}
		
		return matches;
	}
	
}
